<template>
  <div>
    <!-- <div style='width:200px;min-height:50px;position:absolute;top:10px;left:0;z-index:99'>
      <img src="/imgs/logo4.png" alt="" style='width:100%'>
    </div> -->
    <canvas ref="canvas" style='position:fixed;z-index:1;width:100%;height:100%;'></canvas>
    <div class="bg" :style="{'background-image': 'url('+background+')'}"></div>
    <div class="login-container">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <div class="title-container">
          <div class="t-46 text-bold " style="color:#fff">{{description}}</div>
          <div class="t-18 text-gray mt-30">{{company}}</div>
        </div>
        <el-form-item class="mt-30" prop="username">
          <el-input class="form-input" v-model="loginForm.username" :placeholder="'请输入用户名'"
            @keyup.enter.native="nextInput" name="username" type="text" auto-complete="on" autofocus>
            <template slot="suffix">
              <div class="svg-container">
                <svg-icon icon-class="user" />
              </div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item class="mt-30" prop="password">
          <el-input class="form-input" :type="passwordType" v-model="loginForm.password" ref="password"
            :placeholder="'请输入密码'" name="password" auto-complete="on" @keyup.enter.native="handleLogin">
            <template slot="suffix">
              <div class="svg-container" @click="showPwd">
                <svg-icon :icon-class="eyeClass" />
              </div>
            </template>
          </el-input>
        </el-form-item>
        <el-button :loading="loading" type="primary" class="submit-button border-radius mt-20" style="width: 100%;"
          @click.native.prevent="handleLogin">登录
        </el-button>
        <!-- <div class="mt-30">
          <el-button @click="downloadpc()" style="width:380px;" class="submit-button border-radius">
            <svg-icon icon-class="computer" />
            <span class="t-16 text-gray">下载客户端</span>
          </el-button>
          <el-tooltip class="item" effect="dark" placement="right">
            <el-button class="submit-button border-radius">
              <svg-icon icon-class="phone" style="color:#1296db;font-size:20px" />
            </el-button>
            <div slot="content" style="text-align:center">
              <span>扫描二维码下载APP</span>
              <br>
              <br>
              <img style="width:80px;height:80px;" :src="imrUrl" />
            </div>
          </el-tooltip>
        </div> -->
        <div class="copyright mt-40">
          <span>© {{currentYear}} {{copyright}} 版权所有</span>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import QRCode from 'qrcode'
export default {
  name: 'Login',
  data() {
    return {
      baseUrl: this.$api.baseUrl,
      api: this.$api.siteInfo,
      AppUrlShow: false,
      imrUrl: '',
      loginForm: {
        username: '',
        password: '',
      },
      loginRules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
      },
      // background: './imgs/bg.jpg',
      background: '',
      passwordType: 'password',
      loading: false,
      showDialog: false,
      redirect: undefined,
      company: '',
      description: '',
      currentYear: dayjs().year(),
      // copyright: '智慧云上',
      copyright: '',
      clientUrl: null,
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true,
    },
  },
  methods: {
    // 下载pc端
    downloadpc() {
      var a = document.createElement('a')
      a.href = this.clientUrl
      a.target = '_blank'
      a.click()
    },
    downloadAndroid() {
      this.AppUrlShow = !this.AppUrlShow
      let url = `${window.origin}/download/`
      QRCode.toDataURL(url)
        //在这里拿到地址，把它赋值给data里面定义的值imrUrl
        .then((tpian) => {
          this.imrUrl = tpian
        })
        .catch((err) => {
          console.error(err)
        })
    },
    nextInput() {
      this.$refs.password.focus()
    },
    getCompany() {
      this.pageLoading = true
      this.$request(this.api)
        .then((r) => {
          let company = r.find((i) => i.param_name == 'COMPANY_NAME')
          let description = r.find((i) => i.param_name == 'SYSTEM_NAME')
          let background = r.find((i) => i.param_name == 'LOGIN_BACKGROUND')
          let copyright = r.find((i) => i.param_name == 'COPYRIGHT')
          this.company = company ? company.param_value : '第六研究所'
          this.copyright = copyright ? copyright.param_value : '华胜信安'
          this.description = description ? description.param_value : '智能制造平台'
          this.background = background ? background.param_value : './imgs/bg3.jpg'
        })
        .catch(() => {
          this.company = '第六研究所'
          this.description = '智能制造平台'
          this.background = './imgs/bg3.jpg'
          this.copyright = '华胜信安'
          this.pageLoading = false
        })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('LoginByUsername', this.loginForm)
            .then(() => {
              this.loading = false
              this.$message({
                message: '登录成功',
                type: 'success',
              })
              this.$router.push({ path: this.redirect || '/' }).catch((err) => {})
            })
            .catch((e) => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
  computed: {
    eyeClass: function () {
      return this.passwordType ? 'eye-close' : 'eye-open'
    },
  },
  created: function () {
    this.getCompany()
    this.$request(this.$api.baseUrl + 'Platform/app/version/latest', {
      appid: 'electron_client',
    }).then((r) => {
      if (r) this.clientUrl = r.pkg_url
    })
    this.downloadAndroid()
  },
  mounted: function () {
    var canvas = this.$refs.canvas
    canvas.width = window.innerWidth
    canvas.height = window.innerHeight
    var ctx = canvas.getContext('2d')
    var particlesArray = []
    var count = parseInt(((canvas.height / 100) * canvas.width) / 100)
    class Particle {
      constructor(x, y) {
        this.x = x
        this.y = y
        this.directionY = 0.5 - Math.random()
        this.directionX = 0.5 - Math.random()
      }
      update() {
        this.y += this.directionY
        this.x += this.directionX
      }
      draw() {
        ctx.beginPath()
        ctx.arc(this.x, this.y, 2, 0, Math.PI * 2)
        ctx.fillStyle = 'white'
        ctx.fill()
      }
    }

    function createParticle() {
      var x = Math.random() * canvas.width
      var y = Math.random() * canvas.height
      particlesArray.push(new Particle(x, y))
    }

    function handleParticle() {
      for (var i = 0; i < particlesArray.length; i++) {
        var particle = particlesArray[i]
        particle.update()
        particle.draw()
        if (particle.x < 0 || particle.x > canvas.width || particle.y < 0 || particle.y > canvas.height) {
          particlesArray.splice(i, 1)
        }
        for (var j = i; j < particlesArray.length; j++) {
          let dx = null
          let dy = null
          let long = null
          dx = particlesArray[i].x - particlesArray[j].x
          dy = particlesArray[i].y - particlesArray[j].y
          long = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2))
          if (long < 100) {
            ctx.beginPath()
            ctx.strokeStyle = 'rgba(255,255,255,' + (1 - long / 200) + ')'
            ctx.moveTo(particlesArray[i].x, particlesArray[i].y)
            ctx.lineTo(particlesArray[j].x, particlesArray[j].y)
            ctx.lineWidth = 1
            ctx.stroke()
          }
        }
      }
    }

    function draw() {
      ctx.clearRect(0, 0, canvas.width, canvas.height)
      if (particlesArray.length < count) {
        createParticle()
      }
      handleParticle()
    }
    setInterval(() => {
      draw(), 1
    })
  },
}
</script>

<style lang="scss" scoped>
.bg {
  position: fixed;
  width: 100%;
  height: 100%;
  background-position: center;
  background-color: #f7f7f7;
  background-size: cover;
  background-attachment: fixed;
  -webkit-filter: blur(2px);
  -moz-filter: blur(2px);
  -o-filter: blur(2px);
  -ms-filter: blur(2px);
  filter: blur(2px);
}

.login-container {
  background: rgba(0, 0, 0, 0.5);
  width: 600px;
  height: 540px;
  padding: 50px 80px;
  position: absolute;
  border-radius: 36px;
  left: 50%;
  top: 50%;
  margin-left: -300px;
  margin-top: -270px;
  z-index: 10;
}

.svg-container {
  width: 25px;
}

.form-input {
  height: 48px;
  line-height: 48px;
  font-size: 20px;
}
::v-deep .el-input--small .el-input__inner {
  height: 48px;
  line-height: 48px;
  border-radius: 10px;
  border-width: 2px;
}
::v-deep .el-input__suffix {
  right: 15px;
}
.title-container {
  text-align: center;
}

.submit-button {
  height: 48px;
  font-size: 20px;
}

.copyright {
  font-size: 12px;
  color: darkgrey;
  text-align: center;
}
.downCilent {
  position: fixed;
  top: 5%;
  right: 3%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 24px;
  background-color: rgba(0, 0, 0, 0.3);
  padding: 10px;
  border-radius: 5px;
}
.wordstyle {
  position: absolute;
  left: 124px;
  top: 13px;
  font-weight: 600;
  color: #333;
}
.border-radius {
  border-radius: 10px;
}
.downAPP {
  position: fixed;
  top: 15%;
  right: 3%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 24px;
  background-color: rgba(0, 0, 0, 0.3);
  padding: 10px;
  border-radius: 5px;
}
.qecodeAPP {
  position: fixed;
  top: 25%;
  right: 3%;
}
.img {
  width: 30px;
  height: 30px;
  margin-right: 6px;
}
</style>
